package org.androidpn.server.xmpp.packet;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.server.entity.Room;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;

/**
 * packet 游戏房间
 * 
 * @author 蔡琪
 * 
 */
public class RoomPacket extends IQ {

	/**
	 * 游戏操作枚举
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum Operation {
		/** 创建游戏房间 */
		createRoom,
		/** 获取在线房间 */
		getOnlineRooms,
		/** 加入房间 */
		joinRoom,
		/** 退出房间 */
		quitRoom,
		/** 更新房间信息 */
		updateRoom,
		/** 开始游戏 */
		startGame
	}

	/** 游戏房间集合 */
	private List<Room> rooms = new ArrayList<Room>();
	/** 游戏房间信息 */
	private Room room;
	/** 操作信息 */
	private Operation operation;

	public String getChildElementXML() {
		StringBuffer xml = new StringBuffer();
		xml.append("<query xmlns=\"jabber:iq:room\">");
		xml.append("<rooms>");
		for (Room room : rooms) {
			xml.append(createRoomNode(room));
		}
		xml.append("</rooms>");
		if (room != null) {
			xml.append(createRoomNode(room));
		}
		if (operation != null && !operation.equals("")) {
			xml.append("<operation>");
			xml.append(operation);
			xml.append("</operation>");
		}
		xml.append("</query>");
		return xml.toString().replaceAll("\\>\\s*|\t|\r|\n", ">");
	}

	/**
	 * 生成role节点
	 * 
	 * @param role
	 * @return
	 */
	private String createRoomNode(Room room) {
		return "<room>" + StringUtils.escapeNode(room.toXML()) + ("</room>");
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

}
